package com.bluesky.controller;

import com.bluesky.comm.Result;
import com.bluesky.comm.ResultUtils;
import com.bluesky.dto.shardUpload.ShardUploadCompleteRequest;
import com.bluesky.dto.shardUpload.ShardUploadDetailResponse;
import com.bluesky.dto.shardUpload.ShardUploadInitRequest;
import com.bluesky.dto.shardUpload.ShardUploadPartRequest;
import com.bluesky.service.shardUpload.ShardUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/shardUpload")
public class ShardUploadController {

    @Autowired
    private ShardUploadService shardUploadService;

    /**
     * 初始化分片上传
     * @param request
     * @return 分片任务id
     */
    @PostMapping("/init")
    public Result<String> init(@RequestBody ShardUploadInitRequest request)  {
        String shardUploadId = shardUploadService.init(request);
        return ResultUtils.success(shardUploadId);
    }

    /**
     * 上传分片（客户端需遍历上传分片文件）
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("uploadPart")
    public Result<Boolean> uploadPart(ShardUploadPartRequest request) throws IOException {
        shardUploadService.uploadPart(request);
        return ResultUtils.success(true);
    }

    /**
     * 合并分片，完成上传
     * @param request
     * @return
     */
    @PostMapping("complete")
    public Result<Boolean> complete(@RequestBody ShardUploadCompleteRequest request) throws IOException {
        shardUploadService.complete(request);
        return ResultUtils.success(true);
    }

    @GetMapping("detail")
    public Result<ShardUploadDetailResponse> detail(@RequestParam("shardUploadId") String shardUploadId) {
        return ResultUtils.success(shardUploadService.detail(shardUploadId));
    }
}
